""" 아재절단기 (叔叔切割机) - Copyright 2021 SodaLake (苏打湖)"""
import sys
import os
from random import randint
import pygame
from pygame.locals import QUIT, Rect, KEYDOWN, K_q, K_w, K_e
from keyobject import KeyObject
from bar import Bar
from result import Result
from text import text
def getResourcePath(relativePath):
    try:
        basePath = sys._MEIPASS
    except Exception:
        basePath = os.path.abspath(".")
    return os.path.join(basePath, relativePath)



pygame.init()

SURFACE = pygame.display.set_mode((1024, 768))
FPSCLOCK = pygame.time.Clock()

keySound = pygame.mixer.Sound(getResourcePath('res/keypress.wav'))

#for i in pygame.font.get_fonts():
#    print(i)


def main():
    
    sysfont = pygame.font.SysFont("gulim", 30)
    startingMessage = sysfont.render(text.getText('howToPlay'), True, (255, 255, 255))

    numOfKeys = 7
    dt = 0.016
    isStarted = False
    index = 0
    keyObjects = []
    for i in range(0, 8):
        keyObjects.append(KeyObject(((1024-(96 + (numOfKeys-1)*110))/2+i*110, 350)))
    keyObjects[0].activate()

    
    bar = Bar(4, ((1024 - 512) / 2, 600))
    result = Result(((1024 - 512) / 2,  400))
    
    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == KEYDOWN:
                if isStarted:
                    if event.key == pygame.key.key_code(keyObjects[index].key):
                        keySound.play()
                        keyObjects[index].press()
                        if index < (numOfKeys-1) :
                            keyObjects[index+1].activate()
                        index+=1
                    else:
                        result.enable(False)
                        isStarted = False
                else:
                    if(event.key == K_q or event.key == K_w or event.key == K_e):

                        if event.key == K_q:
                            numOfKeys = 7
                            bar.reset(4.0)
                        elif event.key == K_w:
                            numOfKeys = 7
                            bar.reset(2.0)
                        elif event.key == K_e:
                            numOfKeys = 8
                            bar.reset(1.5)
                            
                        isStarted = True
                        index = 0
                        for i in range(0, numOfKeys):
                            keyObjects[i].reset()
                            keyObjects[i].changePos(((1024-(96 + (numOfKeys-1)*110))/2+i*110, 350))
                        keyObjects[0].activate()
                        result.disable()
                         
        ##########################           
                
        if index == numOfKeys:
            isStarted = False
            result.enable(True)
            
            
        if isStarted:
            bar.update(dt)

        if bar.time[0] <= 0:
            isStarted = False
            result.enable(False)

        ########################
        SURFACE.fill((0, 0, 0))
        
        for i in range(0, numOfKeys):
            keyObjects[i].render()

        bar.render()
        result.render()

        if not isStarted:
            SURFACE.blit(startingMessage, startingMessage.get_rect())
        
        ##########################
        
        pygame.display.update()
        dt = FPSCLOCK.tick(60) / 1000

if __name__ == "__main__":
    main()
